library(rio)
library(tidyverse)
library(datawizard)

# This file imports each study, creates D1 (the treatment variable) and Y1 (the outcome)
# we use for our analysis. It then exports each study's dataset into a "cleaned" 
# version that we use for our analysis.

# SET WORKING DIRECTORY TO FOLDER BELOW
setwd("data/raw/")

#### STUDY 1 ####

## IMPORTING AND TIDYING

d <- import("TESS_0001_SHANNON_RECR_VARS_STATA.dta")
# H1: hypotheses related to the fiscal burden theory: that U.S. citizens’ 
# concerns about immigrants trigger exclusionary preferences


# D1 = GROUP (Control = 1, 3 = immigrant; 5 = illegal immigrant)
#     coded so 1 = immigrant; 0 = control
# Y1 = Q1 and Q4 (control), Q7 and Q10 (immigrant condition) 
#     (child tax credit support)

d <- d %>% 
      mutate(D1 = case_when(
            GROUP %in% c(1, 2) ~ 0,
            GROUP %in% c(3, 4) ~ 1, 
            TRUE ~ NA_real_),
            Y1A = na_if(Q1, 98),
            Y1B = na_if(Q7, 98),
            Y1C = na_if(Q10, 98),
            Y1D = na_if(Q4, 98),
            Y1 = case_when(
                  !is.na(Y1A) ~ Y1A,
                  !is.na(Y1B) ~ Y1B,
                  !is.na(Y1C) ~ Y1C,
                  !is.na(Y1D) ~ Y1D)
      )

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####
TESS_001_SHANNON_CLEANED <-
      d %>%
      mutate(StudyId = 001,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT = WEIGHT1, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

TESS_001_SHANNON_CLEANED %>%
      write_csv("../analysis/cleaned/TESS_001_SHANNON_CLEANED.csv")


#### STUDY 2 ####

## IMPORTING AND TIDYING
d <- import("TESS_0002_POWELL_RECR_VARS_STATA.dta")
# Hypothesis 1. In a male-typed product market, a product made by a woman 
# will be evaluated at a discount compared to the same product made by a man.

# D1 = PGENID (gender: 2 = transwoman, 1 = transman) 
#     coded so 0 = transman; 1 = transwoman
# Y1 = Q1A, Q1A_1, Q1B, Q1B_1 (same question, different conditions)
#     which bathroom should person use? 1 = men's room; 2 = women's; 3 = other, specify 

d <- d %>% # dropping cases of "other, please specify" 
      mutate(D1 = case_when(
            PGENCONF == 1 ~ 1, # conforming
            PGENCONF == 2 ~ 0, # non-conforming
            TRUE ~ NA_real_),
            Y1A = na_if(Q1A, 98),
            Y1A = na_if(Y1A, 3),
            Y1A = ifelse(Y1A == 2, 1, 0), # coding so 1 = women's lavatory 
            
            Y1B = na_if(Q1A_1, 98),
            Y1B = na_if(Y1B, 3),
            Y1B = ifelse(Y1B == 1, 1, 0),
            
            Y1C = na_if(Q1B, 98),
            Y1C = na_if(Y1C, 3),
            Y1C = ifelse(Y1C == 2, 1, 0),
            
            Y1D = na_if(Q1B_1, 98),
            Y1D = na_if(Y1D, 3),
            Y1D = ifelse(Y1D == 2, 1, 0),
            
            Y1E = na_if(Q1B_T2, 98),
            Y1E = na_if(Y1E, 3),
            Y1E = ifelse(Y1E == 2, 1, 0),
            
            Y1F = case_when(
                  !is.na(Q1A) ~ Y1A,
                  !is.na(Q1A_1) ~ Y1B,
                  !is.na(Q1B) ~ Y1C,
                  !is.na(Q1B_1) ~ Y1D,
                  !is.na(Q1B_T2) ~ Y1E),
            
            Y1 = case_when(
                  Y1F == 1 & PGENID == 2 ~ 1, # transwoman gender-affirming 
                  Y1F == 0 & PGENID == 1 ~ 1, # transman gender-affirming
                  TRUE ~ 0 # gender-non-affirming
            ) )

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####
Tess_002_clean <-
      d %>%
      mutate(StudyId = 002,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT = WEIGHT1, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, D1, Y1, TNRFU, FORMULA)

Tess_002_clean %>%
      write_csv("../analysis/cleaned/TESS_002_POWELL_CLEANED.csv")

#### STUDY 3 ####

## IMPORTING AND TIDYING
d <- import("TESS_0003_WILLIAMSON_RECR_VARS_STATA.dta")
# D1 = GROUP (3 is the control)
# Y1 = Q9A
# [Food stamps] Should federal spending on the 
# following be increased, decreased, or 
# kept about the same?

d <- d %>% mutate(Y1 = na_if(Q9A, 98), # Drop NAs on Y
                  Y1 = case_when(
                        Y1 == 1 ~ 1, #increased
                        Y1 == 2 ~ -1, #decreased
                        Y1 == 3 ~ 0 # stay same
                  )) 

d$D1 <- ifelse(d$GROUP == 3, 0, d$GROUP)
d$D1 <- na_if(d$D1, 2) # primary hypothesis is condition 1, so 2 is dropped

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####
Tess_003_clean <-
      d %>%
      mutate(StudyId = 003,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT = WEIGHT1, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, D1, Y1, TNRFU, FORMULA)

Tess_003_clean %>%
      write_csv("../analysis/cleaned/TESS_003_WILLIAMSON_CLEANED.csv")

#### STUDY 4 ####

# import
d <- import("TESS_0004_TAK_RECR_VARS_STATA.dta")

d <- d %>% filter(PITEM == 1) %>%
      mutate(Y1A = na_if(Q1, 98),
             Y1B = na_if(Q2, 998),
             Y1C = na_if(Q4, 98),
             Y1D = na_if(Q5, 98),
             
             Y1A = standardize(Y1A),
             Y1B = standardize(Y1B),
             Y1C = standardize(Y1C),
             Y1D = standardize(Y1D),
             
             Y1 = (Y1A + Y1B + Y1C + Y1D)/4,
             
             W1 = na_if(Q7, 98), 
             W2 = ifelse(PAWARD == 1, 1, 0), # 1 = award
             
             D1 = ifelse(PMAKER == 2, 1, 0)) # 1 = woman-made
d$ATTEND <- d$RELIG_1

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))


##### DATA STANDARDIZATION #####

Tess_004_clean <-
      d %>%
      mutate(StudyId = 004,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT = WEIGHT1, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_004_clean %>%
      write_csv("../analysis/cleaned/TESS_004_TAK_CLEANED.csv")

#### STUDY 5 ####
## IMPORTING AND TIDYING
d <- import("TESS_0005_FARROW_RECR_VARS_STATA.dta")

d <- d %>% 
      mutate(D1 = case_when(
            SCENE == 1 ~ 0,
            SCENE == 2 ~ 1,
            TRUE ~ NA_real_),
            
            Y1A = na_if(S1, 98),
            Y1B = na_if(S2, 98),
            Y1 = case_when(
                  !is.na(Y1A) ~ Y1A,
                  !is.na(Y1B) ~ Y1B))

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_005_clean <-
      d %>%
      mutate(StudyId = 005,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT = WEIGHT1, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_005_clean %>%
      write_csv("../analysis/cleaned/TESS_005_FARROW_CLEANED.csv")


#### STUDY 6 ####

## IMPORTING AND TIDYING
d <- import("TESS_0006_WALLACE_RECR_VARS_STATA.dta")

d <- d %>% 
      mutate(Y1 = na_if(Q10, 98),
             Y1 = 8 - Y1, # larger = more approval
             Y2 = na_if(Q11, 98),
             Y2 = 8 - Y2,
             
             D1 = case_when( 
                   P_FORCE == 1 ~ 0,
                   P_FORCE == 2 ~ 1),
             
             D2 = case_when(
                   P_OBLIGE == 2 ~ 0,
                   P_OBLIGE == 4 ~ 1)) 

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0 & D2 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_006_clean <-
      d %>%
      mutate(StudyId = 006,
             FORMULA = "Y1 ~ D1*D2") %>%
      select(StudyId, WEIGHT = WEIGHT1, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, Y2, D1, D2, TNRFU, FORMULA)

Tess_006_clean %>%
      write_csv("../analysis/cleaned/TESS_006_WALLACE_CLEAN.csv")



#### STUDY 7 ####

## IMPORTING AND TIDYING
d <- import("TESS_0007_HAALAND_RECR_VARS_STATA.dta")

d <- d %>% 
      mutate(D1 = ifelse(GROUP == 1, 1, 0), # 1 = military work history
             Y2A = ifelse(Q7A == "Q7_Org1", 1, 0),
             Y2B = ifelse(Q7B == "Q7_Org2", 1, 0),
             Y2C = ifelse(Q7C == "Q7_Org3", 1, 0),
             Y2D = ifelse(Q7D == "Q7_Org4", 1, 0),
             Y2E = ifelse(Q7E == "Q7_Org5", 1, 0),
             Y2F = ifelse(Q7F == "Q7_Org6", 1, 0),
             Y2 = Y2A + Y2B + Y2C + Y2D + Y2E + Y2F,
             Y2 = (Y2 - mean(Y2[D1==0], na.rm = T)) / sd(Y2[D1==0], na.rm = T),
             Y1 = na_if(Q3, 98),
             Y3 = na_if(Q4, 98),
             M1 = na_if(Q2, 98),
             W1 = na_if(Q1, 9998),
             W1 = case_when(
                   between(Q1, 39, 43) ~ NA_real_,
                   Q1 < 39 ~ 0,
                   Q1 > 43 ~ 1)) 

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_007_clean <-
      d %>%
      mutate(StudyId = 007,
             FORMULA = "Y1 ~ D1*W1") %>%
      select(StudyId, WEIGHT = WEIGHT1, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, W1, TNRFU, FORMULA)

Tess_007_clean %>%
      write_csv("../analysis/cleaned/TESS_007_HAALAND_CLEAN.csv")

#### STUDY 8 ####
## IMPORTING AND TIDYING
d <- import("TESS_0008_MUTZ_RECR_VARS_STATA.dta")

d <- d %>% 
      mutate(D1 = case_when(
            P_TREATMENT == 2 ~ 1,
            P_TREATMENT == 3 ~ 0,
            TRUE ~ NA_real_),
            
            Y1A = na_if(Q2A, 98),
            Y1B = na_if(Q2B, 98),
            Y1C = na_if(Q2C, 98),
            Y1 = 6 - ((Y1A + Y1B + Y1C) /3),
            
            PartyID7 = PARTYID7)

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

#### DATA STANDARDIZATION ####

Tess_008_clean <-
      d %>%
      mutate(StudyId = 008,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT = WEIGHT1, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7 = PARTYID7,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, ATTEND,
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_008_clean %>%
      write_csv("../analysis/cleaned/TESS_008_MUTZ_CLEAN.csv")


#### STUDY 9 ####
## IMPORTING AND TIDYING
norc <- import("TESS_0009_BAUM_RECR_VARS_STATA.dta")
load("../extra/TESS_0009_main.study.data.RData") # data stored in "d"

norc <- select(norc, CaseId, WEIGHT, AGE, GENDER, RACETHNICITY,EDUC, MARITAL, ATTEND,
               EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7 = P_PARTYID7,
               HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
               HH01, HH25, HH612, HH1317, HH18OV, RespID, TNRFU)

# join norc data set to their replication data to get TNRFU
d <- select(d, RAPE, REPORT, CHOSEN, VIC_ETHN, RESPID)
d <- left_join(d, norc, by = c("RESPID" = "RespID"))


# just the condition corresponding to H1
d <- subset(d, d$RAPE==1 & d$REPORT==1)       #Rape Reporting

d <- d %>% 
      mutate(Y1 = CHOSEN, 
             D1 = ifelse(VIC_ETHN == "Victim White", 0, 1))

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION ##### 

Tess_009_clean <-
      d %>%
      mutate(StudyId = 009,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_009_clean %>%
      write_csv("../analysis/cleaned/TESS_009_BAUM_CLEAN.csv")

#### STUDY 11 ####
## IMPORTING AND TIDYING
d <- import("TESS_0011_BOUGHER_RECR_VARS_STATA.dta")

d <- d %>%
      mutate(D1 = case_when(
            P_CUES == 3 & P_POLARIZE == 1 ~ 0, 
            P_CUES == 3 & P_POLARIZE > 1 ~ 1, # 2/3 = partial agreement with outparty
            TRUE ~ NA_real_),
            Y1A = na_if(Q5A, 998),
            Y1B = na_if(Q5B, 998),
            Y1 = abs(Y1A - Y1B),
            Y2A = na_if(Q3A, 998),
            Y2B = na_if(Q3B, 998),
            Y2 = abs(Y2A - Y2B))

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_011_clean <-
      d %>%
      mutate(StudyId = 011,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT =  WEIGHT1, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, Y2, D1, TNRFU, FORMULA)

Tess_011_clean %>%
      write_csv("../analysis/cleaned/TESS_011_BOUGHER_CLEAN.csv")


#### STUDY 12 ####

## IMPORTING AND TIDYING
d <- import("TESS_0012_SIMAS_RECR_VARS_STATA.dta")

d <- d %>% mutate(likability = case_when(
      !is.na(Q6) ~ Q6,
      !is.na(Q14) ~ Q14
))

d <- d %>% mutate(pre.vote = case_when(
      !is.na(Q1) ~ Q1,
      !is.na(Q11) ~ Q11
))

d$likability <- na_if(d$likability, 50)
d$pre.vote <- na_if(d$pre.vote, 50)

d <- d %>% mutate(agreement = case_when(
      # transgender issue
      pre.vote == 1 & RND_03 == 0 ~ 1, 
      pre.vote == 2 & RND_03 == 1 ~ 1, 
      pre.vote == 1 & RND_03 == 1 ~ 0, 
      pre.vote == 2 & RND_03 == 0 ~ 0,
      
      # business issue
      pre.vote == 1 & RND_04 == 0 ~ 1, 
      pre.vote == 2 & RND_04 == 1 ~ 1, 
      pre.vote == 1 & RND_04 == 1 ~ 0, 
      pre.vote == 2 & RND_04 == 0 ~ 0,
))

# re-creating treatment variable authors use in .do file
# PARTYID2: 1 = Dem; 2 = GOP
d <- d %>% mutate(treatment_char = case_when(
      # transgender issue
      PARTYID2 == 1 & RND_02 == 0 & RND_03 == 1 ~ "maintains", # Dem clear against
      PARTYID2 == 1 & RND_02 == 0 & RND_03 == 0 ~ "changes", # clear for
      PARTYID2 == 1 & RND_02 == 1 & RND_03 == 0 ~ "ambiguous", # for
      PARTYID2 == 1 & RND_02 == 1 & RND_03 == 1 ~ "ambiguous", # against
      
      PARTYID2 == 2 & RND_02 == 0 & RND_03 == 0 ~ "maintains", # GOP clear for
      PARTYID2 == 2 & RND_02 == 0 & RND_03 == 1 ~ "changes",
      PARTYID2 == 2 & RND_02 == 1 & RND_03 == 0 ~ "ambiguous",
      PARTYID2 == 2 & RND_02 == 1 & RND_03 == 1 ~ "ambiguous",
      
      # business issue
      PARTYID2 == 1 & RND_02 == 0 & RND_04 == 1 ~ "maintains", # Dem clear against
      PARTYID2 == 1 & RND_02 == 0 & RND_04 == 0 ~ "changes",
      PARTYID2 == 1 & RND_02 == 1 & RND_04 == 0 ~ "ambiguous",
      PARTYID2 == 1 & RND_02 == 1 & RND_04 == 1 ~ "ambiguous",
      
      PARTYID2 == 2 & RND_02 == 0 & RND_04 == 0 ~ "maintains", # GOP clear for
      PARTYID2 == 2 & RND_02 == 0 & RND_04 == 1 ~ "changes",
      PARTYID2 == 2 & RND_02 == 1 & RND_04 == 0 ~ "ambiguous",
      PARTYID2 == 2 & RND_02 == 1 & RND_04 == 1 ~ "ambiguous",
))

d <- d %>% mutate(treatment = case_when(
      treatment_char == "maintains" ~ 2,
      treatment_char == "changes" ~ 1,
      treatment_char == "ambiguous" ~ 0,
))


d <- d  %>%
      mutate(D1 = case_when(
            treatment_char == "changes" & agreement == 0 ~ 1,
            treatment_char == "ambiguous" & agreement == 0 ~ 0,
            TRUE ~ NA_real_),
            Y1 = likability,
            PartyID7 = PARTYID7)

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

TESS_012_CLEAN <-
      d %>%
      mutate(StudyId = 012,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)
TESS_012_CLEAN %>%
      write_csv("../analysis/cleaned/TESS_012_SIMAS_CLEAN.csv")


#### STUDY 13 ####

## IMPORTING AND TIDYING
d <- import("TESS_0013_AHLER_RECR_VARS_STATA.dta")

d <- d %>%
      mutate(D1 = case_when(
            P_TESS13 == 1 ~ 0,
            P_TESS13 == 2 ~ 1,
            TRUE ~ NA_real_),
            Y1A = 5 - na_if(Q6_A, 98),
            Y1B = 6 - na_if(Q6_B, 98),
            Y1C = 6 - na_if(Q6_C, 98),
            Y1D = 6 - na_if(Q6_D, 98),
            Y1A = standardize(Y1A),
            Y1B = standardize(Y1B),
            Y1C = standardize(Y1C),
            Y1D = standardize(Y1D),
            Y1 = (Y1A + Y1B + Y1C + Y1D)/4,
            PartyID7 = PARTYID7)

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_013_clean <-
      d %>%
      mutate(StudyId = 013,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT = WEIGHT1, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7 = PARTYID7,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, ATTEND,
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_013_clean %>%
      write_csv("../analysis/cleaned/TESS_013_AHLER_CLEAN.csv")

#### STUDY 14 ####

d <- import("TESS_0014_SCHNABEL_RECR_VARS_STATA.dta")

d <- d %>% 
      mutate(D1 = ifelse(
            P_TESS14_1 %in% c(1,5), 1, 0),
            Y1 = na_if(Q1, 98),
            Y2 = na_if(Q2, 98))

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

Tess_014_clean <-
      d %>%
      mutate(StudyId = 014,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, Y2, D1, TNRFU, FORMULA)

Tess_014_clean %>%
      write_csv("../analysis/cleaned/TESS_014_SCHNABEL_CLEAN.csv")


#### STUDY 15 ####

d <- import("TESS_0015_CHENG_RECR_VARS_STATA.dta")

d <- d %>% mutate(Y1A = na_if(Q1_1, 998),
                  Y1B = na_if(Q3_1, 998),
                  Y1C = na_if(Q4_1, 998),
                  Y1 = case_when(
                        !is.na(Y1A) ~ Y1A,
                        !is.na(Y1B) ~ Y1B,
                        !is.na(Y1C) ~ Y1C),
                  Y2A = na_if(Q1_2, 998),
                  Y2B = na_if(Q3_2, 998),
                  Y2C = na_if(Q4_2, 998),
                  Y2 = case_when(
                        !is.na(Y2A) ~ Y2A,
                        !is.na(Y2B) ~ Y2B,
                        !is.na(Y2C) ~ Y2C),
                  Y3A = na_if(Q1_3, 998),
                  Y3B = na_if(Q3_3, 998),
                  Y3C = na_if(Q4_3, 998),
                  Y3 = case_when(
                        !is.na(Y3A) ~ Y3A,
                        !is.na(Y3B) ~ Y3B,
                        !is.na(Y3C) ~ Y3C),
                  X1A = case_when(
                        !is.na(Q1PrcntShownFrst) ~ Q1PrcntShownFrst,
                        !is.na(Q3PrcntShownFrst) ~ Q3PrcntShownFrst,
                        !is.na(Q4PrcntShownFrst) ~ Q4PrcntShownFrst),
                  X1B = case_when(
                        !is.na(Q1PrcntShownSnd) ~ Q1PrcntShownSnd,
                        !is.na(Q3PrcntShownSnd) ~ Q3PrcntShownSnd,
                        !is.na(Q4PrcntShownSnd) ~ Q4PrcntShownSnd),
                  X1C = case_when(
                        !is.na(Q1PrcntShownTrd) ~ Q1PrcntShownTrd,
                        !is.na(Q3PrcntShownTrd) ~ Q3PrcntShownTrd,
                        !is.na(Q4PrcntShownTrd) ~ Q4PrcntShownTrd),
                  D1 = case_when(
                        P_CONDITION == 1 ~ 0, # control
                        P_CONDITION == 3 ~ 1, # hard-working kid
                        P_CONDITION == 4 ~ 2, # college graduate kid
                        TRUE ~ NA_real_))


d$D1 <- na_if(d$D1, 2) # keeping only one condition, hard-working, since it comes first

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_015_clean <-
      d %>%
      mutate(StudyId = 015,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_015_clean %>%
      write_csv("../analysis/cleaned/TESS_015_CHENG_CLEAN.csv")

#### STUDY 16 ####

## IMPORTING AND TIDYING
d <- import("TESS_0016_DIETZE_RECR_VARS_STATA.dta")

d$Q16A <- na_if(d$Q16A, 98)
d$Q16B <- na_if(d$Q16B, 98)
d <- mutate(d, Y1 = (Q16A + Q16B) /2,
            D1 = ifelse(INFO == 2, 1, 0))

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))


#### DATA STANDARDIZATION

Tess_016_clean <-
      d %>%
      mutate(StudyId = 016,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT = WEIGHT1, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)


Tess_016_clean %>%
      write_csv("../analysis/cleaned/TESS_016_DIETZE_CLEAN.csv")


#### STUDY 18 ####

## IMPORTING AND TIDYING
d <- import("TESS_0018_MCCABE_RECR_VARS_STATA.dta")

d <- d %>%
      mutate(D1 = case_when(
            EXPM == 1 ~ 0,
            EXPM == 4 ~ 1,
            TRUE ~ NA_real_),
            Y1A = na_if(Q11D, 98),
            Y1 = 6 - Y1A)

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))
d <- filter(d, !is.na(WEIGHT))

##### DATA STANDARDIZATION #####

Tess_018_clean <-
      d %>%
      mutate(StudyId = 018,
             FORMULA = "Y1 ~ D1") %>%
      dplyr::select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
                    EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
                    HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
                    HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_018_clean %>%
      write_csv("../analysis/cleaned/TESS_018_MCCABE_CLEAN.csv")

#### STUDY 19 ####

## IMPORTING AND TIDYING
d <- import("TESS_0019_RYAN_RECR_VARS_STATA.dta")

d$Y1 <- na_if(d$Q2, 98) 
d$Y1 <- normalize(d$Y1) 
d$Y1 <- 1 - d$Y1 

d$W1 <- na_if(d$PartyID7, -1) 
d <- d %>% mutate(W1 = case_when( 
      W1 < 4 ~ 0,
      W1 > 4 ~ 1,
      W1 == 4 ~ NA_real_)) 

d <- d %>% mutate(D1 = case_when(
      W1 == 0 & Q2_INSERT == 0 ~ 1,
      W1 == 0 & Q2_INSERT == 1 ~ 0,
      W1 == 1 & Q2_INSERT == 1 ~ 1,
      W1 == 1 & Q2_INSERT == 0 ~ 0
))

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_019_clean <-
      d %>%
      mutate(StudyId = 019,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, W1, TNRFU, FORMULA)

Tess_019_clean %>%
      write_csv("../analysis/cleaned/TESS_019_Ryan_CLEAN.csv")


#### STUDY 20 ####


## IMPORTING AND TIDYING
d <- import("TESS_0020_BANDARA_RECR_VARS_STATA.dta")

d <- d %>% mutate(Y1 = na_if(Q4, 98),
                  D1 = case_when(
                        P_TESS020 == 0 ~ 0,
                        P_TESS020 == 2 ~ 1, 
                        TRUE ~ NA_real_))

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_020_clean <-
      d %>%
      mutate(StudyId = 020,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_020_clean %>%
      write_csv("../analysis/cleaned/TESS_020_BANDARA_CLEAN.csv")

#### STUDY 21 ####

## IMPORTING AND TIDYING
d <- import("TESS_0021_CHU_RECR_VARS_STATA.dta")

d <- d %>% mutate(Y1 = na_if(Q2, 98),
                  D1 = case_when(
                        SCENARIO <= 2 ~ 1, 
                        SCENARIO > 2 ~ 0,
                        TRUE ~ NA_real_)) 

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

#### DATA STANDARDIZATION #####

Tess_021_clean <-
      d %>%
      mutate(StudyId = 021,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT = WEIGHT1, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_021_clean %>%
      write_csv("../analysis/cleaned/TESS_021_CHU_CLEAN.csv")

#### STUDY 22 ####

## IMPORTING AND TIDYING
d <- import("TESS_0022_MIRELES_RECR_VARS_STATA.dta")

d <- d %>% mutate(Y1 = na_if(AAM06, 98),
                  Y2 = na_if(AAM07, 98),
                  Y3 = (Y1 + Y2)/2,
                  D1 = case_when(
                        P_AAM02 == 2 & P_AAM05 == 2 ~ 0, # control
                        P_AAM02 == 1 & P_AAM05 == 1 ~ 1))

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_022_clean <-
      d %>%
      mutate(StudyId = 022,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, Y2, Y3, D1, TNRFU, FORMULA)

Tess_022_clean %>%
      write_csv("../analysis/cleaned/TESS_022_MIRELES_CLEAN.csv")

#### STUDY 23 ####

## IMPORTING AND TIDYING
d <- import("TESS_0023_KENNEDY_RECR_VARS_STATA.dta")

d <- d %>% mutate(Y1 = na_if(Q1, 98),
                  Y2 = na_if(Q2, 98),
                  Y3 = na_if(Q4, 98),
                  D1 = case_when(
                        P_TESS23 == 2 ~ 0, # control
                        P_TESS23 == 4 ~ 1,
                        TRUE ~ NA_real_))

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_023_clean <-
      d %>%
      mutate(StudyId = 023,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, Y2, Y3, D1, TNRFU, FORMULA)

Tess_023_clean %>%
      write_csv("../analysis/cleaned/TESS_023_KENNEDY_CLEAN.csv")


#### STUDY 24 ####
## IMPORTING AND TIDYING
d <- import("TESS_0024_HANKINSON_RECR_VARS_STATA.dta")


d$Q2 <- na_if(d$Q2, 98) 
d$Q2 <- 6 - d$Q2 
d$Y1 <- ifelse(d$Q2 >= 3, 1, 0)
d$D1 <- ifelse(d$P_DISTANCE == 1, 1, 0)
d$PartyID7 <- d$P_PARTYID7
d$ATTEND <- d$DOV_ATTEND
d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_024_clean <-
      d %>%
      mutate(StudyId = 024,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_024_clean %>%
      write_csv("../analysis/cleaned/TESS_024_HANKINSON_CLEAN.csv")



#### STUDY 25 ####
## IMPORTING AND TIDYING
d <- import("TESS_0025_TERMAN_RECR_VARS_STATA.dta")

d <- d %>% mutate(Y1 = na_if(Q4_A, 98),
                  D1 = case_when(
                        !is.na(Q2A) ~ 0, 
                        !is.na(Q2B) ~ 1, 
                        TRUE ~ NA_real_),
                  W1 = PartyID7)

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_025_clean <-
      d %>%
      mutate(StudyId = 025,
             FORMULA = "Y1 ~ D1*W1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, W1, TNRFU, FORMULA)

Tess_025_clean %>%
      write_csv("../analysis/cleaned/TESS_025_TERMAN_CLEAN.csv")


#### STUDY 27 ####

## IMPORTING AND TIDYING
d <- import("TESS_0027_HARBRIDGEYONG_RECR_VARS_STATA.dta")

d$Q3 <- na_if(d$Q3, 98)
d$Y1 <- normalize(d$Q3) 

d <- d %>% mutate(D1 = case_when(
      DOV_VIG %in% c(1, 4) ~ 0, 
      DOV_VIG %in% c(2, 5, 6) ~ 1,
      DOV_VIG %in% c(3, 7, 8) ~ NA_real_ 
))


d <- d %>% mutate(D2 = case_when(
      DOV_VIG %in% c(1, 4) ~ 0, 
      DOV_VIG %in% c(2, 5, 6) ~ NA_real_,
      DOV_VIG %in% c(3, 7, 8) ~ 1 
))

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_027_clean <-
      d %>%
      mutate(StudyId = 027,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_027_clean %>%
      write_csv("../analysis/cleaned/TESS_027_HARBRIDGE-YONG_CLEAN.csv")


#### STUDY 28 ####

## IMPORTING AND TIDYING
d <- import("TESS_0028_SHANNON_RECR_VARS_STATA.dta")

d <- d %>% mutate(Y1A = na_if(Q1, 98),
                  Y1B = na_if(Q2, 98),
                  Y1 = case_when(
                        !is.na(Y1A) ~ Y1A,
                        !is.na(Y1B) ~ Y1B),
                  PartyID7 = PARTYID7,
                  D1 = case_when(
                        !is.na(Y1A) ~ 0, # anti-immi from Clinton (I think)
                        !is.na(Y1B) ~ 1 # pro-immi from Reagan
                  ))

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_028_clean <-
      d %>%
      mutate(StudyId = 028,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_028_clean %>%
      write_csv("../analysis/cleaned/TESS_028_SHANNON_CLEAN.csv")


#### STUDY 29 ####

## IMPORTING AND TIDYING
d <- import("TESS_0029_HOWAT_RECR_VARS_STATA.dta")

d <- d %>% mutate(D1 = case_when(
      P_EXP29 == 1 ~ 1,
      P_EXP29 == 2 ~ 0)) %>%
      mutate(Y1A = na_if(Q2_D, 998),
             Y1B = na_if(Q2_R, 998),
             Y1 = abs(Y1A - Y1B),
             Y2A = na_if(Q3_1, 98), 
             Y2B = na_if(Q3_3, 98),
             PartyID7 = P_PARTYID7,
             Y2 = abs(Y2A - Y2B))

d$ATTEND <- d$P_ATTEND
d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_029_clean <-
      d %>%
      mutate(StudyId = 029,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, Y2, D1, TNRFU, FORMULA)

Tess_029_clean %>%
      write_csv("../analysis/cleaned/TESS_029_HOWAT_CLEAN.csv")


#### STUDY 30 ####

## IMPORTING AND TIDYING
d <- import("TESS_0030_MORGAN_RECR_VARS_STATA.dta")

d$P_CONDITION <- na_if(d$P_CONDITION, 3) 
d <- d %>% mutate(D1 = ifelse(P_CONDITION == 2, 1, 0))

d <- d %>% mutate(Y1 = case_when(
      !is.na(Q1) ~ 2-Q1, 
      !is.na(Q3A) ~ 2-Q3A
))
d$Y1 <- na_if(d$Y1, -96)
d$PartyID7 <- d$PARTYID7

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_030_clean <-
      d %>%
      mutate(StudyId = 030,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_030_clean %>%
      write_csv("../analysis/cleaned/TESS_030_MORGAN_CLEAN.csv")


#### STUDY 31 ####

## IMPORTING AND TIDYING
d <- import("TESS_0031_SILVERMAN_RECR_VARS_STATA.dta")

d <- d %>% mutate(Y1 =  5 - na_if(Q6C, 98),
                  D1 = case_when(
                        P_TESS031 == 1 ~ 0,
                        P_TESS031 == 2 ~ 1,
                        P_TESS031 == 5 ~ 1,
                        TRUE ~ NA_real_
                  ),
                  PartyID7 = PARTYID7)


d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_031_clean <-
      d %>%
      mutate(StudyId = 031,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_031_clean %>%
      write_csv("../analysis/cleaned/TESS_031_SILVERMAN_CLEAN.csv")


#### STUDY 32 ####
## IMPORTING AND TIDYING
d <- import("TESS_0032_YADON_RECR_VARS_STATA.dta")

d <- d %>% mutate(W1 = na_if(Q1, 98),
                  W1 = case_when(
                        Q1 <= 4 ~ 0, 
                        Q1 >= 8 & Q1 < 11 ~ 1)) %>%
      mutate(Y1 = na_if(Q7_1, 98)) %>%
      mutate(D1 = case_when(
            DOV_T32 == 5 ~ 0,
            DOV_T32 == 2 ~ 1,
            TRUE ~ NA_real_
      )) %>%
      mutate(Y2 = na_if(Q7, 98)) %>%
      mutate(W2 = na_if(Q3, 98)) %>%
      mutate(PartyID7 = PARTYID7)

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_032_clean <-
      d %>%
      mutate(StudyId = 032,
             FORMULA = "Y1 ~ D1*W1*W2") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, Y2, D1, W1, W2, TNRFU, FORMULA)

Tess_032_clean %>%
      write_csv("../analysis/cleaned/TESS_032_YADON_CLEAN.csv")


#### STUDY 33 ####
## IMPORTING AND TIDYING
d <- import("TESS_0033_HAMILTON_RECR_VARS_STATA.dta")

d <- d %>%
      mutate(D1 = case_when(
            HAMILTON_VIGNETTE == 1 ~ 0,
            HAMILTON_VIGNETTE == 2 ~ 1,
            TRUE ~ NA_real_),
            PartyID7 = PARTYID7,
            Y1 = na_if(Q1A, 98))

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))
d$Y1 <- d$Y1/2

##### DATA STANDARDIZATION #####

Tess_033_clean <-
      d %>%
      mutate(StudyId = 033,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_033_clean %>%
      write_csv("../analysis/cleaned/TESS_033_HAMILTON_CLEAN.csv")


#### STUDY 34 ####

## IMPORTING AND TIDYING
d <- import("TESS_0034_BROWER_RECR_VARS_STATA.dta")

d <- d %>% mutate(Y1 = na_if(Q1, 98)) %>%
      mutate(D1 = case_when( 
            P_BROWER == 1 ~ 1,
            P_BROWER == 2 ~ 0)) %>%
      mutate(D2 = case_when( 
            P_BROWER == 1 ~ 1,
            P_BROWER == 4 ~ 0),
            PartyID7 = PARTYID7)

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_034_clean <-
      d %>%
      mutate(StudyId = 034,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_034_clean %>%
      write_csv("../analysis/cleaned/TESS_034_BROWER_CLEAN.csv")

#### STUDY 35 ####

## IMPORTING AND TIDYING
d <- import("TESS_0035_KRUPNIKOV_RECR_VARS_STATA.dta")

d$Q5 <- na_if(d$Q5, 98)
d <- d %>% 
      mutate(Y1 = Q5)

d <- d %>% mutate(D1 = ifelse(FEEL_THINK > 2, 1, 0)) 
d <- d %>% mutate(W1 = ifelse(GENDER == 2, 1, 0))
d$PartyID7 <- d$PARTYID7
d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_035_clean <-
      d %>%
      mutate(StudyId = 035,
             FORMULA = "Y1 ~ D1*W1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, W1, TNRFU, FORMULA)

Tess_035_clean %>%
      write_csv("../analysis/cleaned/TESS_035_KRUPNIKOV_CLEAN.csv")


#### STUDY 36 ####

## IMPORTING AND TIDYING
d <- import("TESS_0036_CALARCO_RECR_VARS_STATA.dta")

d$Q7 <- na_if(d$Q7, 98)

d <- d %>%
      mutate(low.status = case_when(
            CALARCO_VIGNETTE %in% c(7, 8, 11, 12) ~ 1,
            CALARCO_VIGNETTE %in% c(1, 2, 5, 6) ~ 0,
            TRUE ~ NA_real_),
            heavy.drinking = case_when(
                  CALARCO_VIGNETTE %in% c(5, 6, 11, 12) ~ 1,
                  CALARCO_VIGNETTE %in% c(1, 2, 7, 8) ~ 0,
                  TRUE ~ NA_real_),
            Y1 = Q7) %>%
      mutate(D1 = heavy.drinking,
             D2 = low.status,
             PartyID7 = PARTYID7)

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))
d$Y1 <- d$Y1 / 2 

##### DATA STANDARDIZATION #####

Tess_036_clean <-
      d %>%
      mutate(StudyId = 036,
             FORMULA = "Y1 ~ D1*D2") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, D2, TNRFU, FORMULA)

Tess_036_clean %>%
      write_csv("../analysis/cleaned/TESS_036_CALARCO_CLEAN.csv")



#### STUDY 37 ####

## IMPORTING AND TIDYING
d <- import("TESS_0037_RIFKIN_RECR_VARS_STATA.dta")

d <- d %>% mutate(Q2A = na_if(Q2A, 98),
                  Q2B = na_if(Q2B, 98),
                  Q3A = na_if(Q3A, 98),
                  Q3B = na_if(Q3B, 98),
                  Q4A = na_if(Q4A, 98),
                  Q4B = na_if(Q4B, 98),
                  Q5A = na_if(Q5A, 98),
                  Q5B = na_if(Q5B, 98),
                  Y1 = (Q2A + Q2B + Q3A + Q3B + Q4A + Q4B + Q5A + Q5B) / 8,
                  D1 = ifelse(BUSY == 2, 1, 0),
                  W1 = AGE,
                  PartyID7 = PARTYID7)

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_037_clean <-
      d %>%
      mutate(StudyId = 037,
             FORMULA = "Y1 ~ D1*W1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, W1, TNRFU, FORMULA)

Tess_037_clean %>%
      write_csv("../analysis/cleaned/TESS_037_RIFKIN_CLEAN.csv")


#### STUDY 39 ####

## IMPORTING AND TIDYING
d <- import("TESS_0039_HANKINSON_RECR_VARS_STATA.dta")

d <- d %>% 
      mutate(D1 = ifelse(
            P_039 %in% c(1:3, 7:9), 1, 0),
            Y1 = na_if(Q1, 98),
            Y1 = ifelse(Y1 < 3, 1, 0), 
            Y2 = na_if(Q2, 98),
            Y2 = ifelse(Y2 < 3, 1, 0), 
            W1 = case_when(
                  RACETHNICITY == 1 ~ 1, 
                  RACETHNICITY == 2 ~ 0,
                  TRUE ~ NA_real_),
            PartyID7 = P_PARTYID)

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

d$ATTEND <- d$P_ATTEND

##### DATA STANDARDIZATION #####

Tess_039_clean <-
      d %>%
      mutate(StudyId = 039,
             FORMULA = "Y1 ~ D1*W1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, Y2, D1, W1, TNRFU, FORMULA)

Tess_039_clean %>%
      write_csv("../analysis/cleaned/TESS_039_HANKINSON_CLEAN.csv")


#### STUDY 40 ####

## IMPORTING AND TIDYING
d <- import("TESS_0040_THORSON_RECR_VARS_STATA.dta")

d <- d %>% mutate(Y1A = na_if(Q6_1_THORSONC, 98),
                  Y1B = na_if(Q6_1_THORSOND, 98),
                  Y1C = na_if(Q6_2_THORSONC, 98),
                  Y1D = na_if(Q6_2_THORSOND, 98),
                  Y1E = (Y1A + Y1B)/ 2,
                  Y1F = (Y1C + Y1D)/ 2,
                  Y1 = case_when(
                        !is.na(Y1E) ~ Y1E,
                        !is.na(Y1F) ~ Y1F),
                  D1 = ifelse(P_THORSON_Q5_Q6 == 2, 1, 0),
                  PartyID7 = P_PARTYID)

d$ATTEND <- d$P_ATTEND

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_040_clean <-
      d %>%
      mutate(StudyId = 040,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_040_clean %>%
      write_csv("../analysis/cleaned/TESS_040_THORSON_CLEAN.csv")


#### STUDY 41 ####

## IMPORTING AND TIDYING
d <- import("TESS_0041_MELIN_RECR_VARS_STATA.dta")

d <- d %>% 
      mutate(Y1 = na_if(Q4_1, 98),
             D1 = ifelse(P_41 <= 3, 1, 0), # 1 = military work history
             D2 = case_when(
                   P_41 %in% c(1,4) ~ 1, # automotive job
                   P_41 %in% c(2,5) ~ 0 # applying for wedding job
             ),
             PartyID7 = P_PARTYID) 
d$ATTEND <- d$P_ATTEND

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_041_clean <-
      d %>%
      mutate(StudyId = 041,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7 = P_PARTYID,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, ATTEND,
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_041_clean %>%
      write_csv("../analysis/cleaned/TESS_041_MELIN_CLEAN.csv")


#### STUDY 42 ####
## IMPORTING AND TIDYING

d <- import("TESS_0042_VOGLER_RECR_VARS_STATA.dta")

d <- d %>% 
      mutate(Y1 = na_if(V1B, 998),
             Y2 = na_if(V1C, 998),
             
             D1 = ifelse(P_GENDER == 1, 1, 0), # 1= man
             D2 = ifelse(P_BEHAV == 1, 1, 0)) # 1 = homosexual
d$PartyID7 = d$P_PARTYID7
d$ATTEND <- d$P_ATTEND
d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0 & D2 == 0], na.rm = T))


#### DATA STANDARDIZATION #####

Tess_042_clean <-
      d %>%
      mutate(StudyId = 042,
             FORMULA = "Y1 ~ D1*D2") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, D2, TNRFU, FORMULA)

Tess_042_clean %>%
      write_csv("../analysis/cleaned/TESS_042_VOGLER_CLEAN.csv")


#### STUDY 43 ####

## IMPORTING AND TIDYING
d <- import("TESS_0043_KLAR_RECR_VARS_STATA.dta")

d <- d %>% mutate(D1 = case_when(
      P_KLAR == 1 ~ 0,
      P_KLAR == 3 ~ 1, # partisan frame
      TRUE ~ NA_real_)) %>%
      mutate(Y1 = na_if(Q2_KLAR, 98)) %>% 
      mutate(D2 = case_when( # gender frame
            P_KLAR == 1 ~ 0,
            P_KLAR == 2 ~ 1,
            TRUE ~ NA_real_)) %>%
      mutate(W1 = na_if(P_IDEO, 8))
d$PartyID7 <- d$P_PARTYID
d$ATTEND <- d$P_ATTEND

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_043_clean <-
      d %>%
      mutate(StudyId = 043,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_043_clean %>%
      write_csv("../analysis/cleaned/TESS_043_KLAR_CLEAN.csv")

#### STUDY 44 ####
## IMPORTING AND TIDYING
d <- import("TESS_0044_COHEN_RECR_VARS_STATA.dta")

d <- d %>% mutate(D1 = ifelse(VIGNOE %in% c(1,2,5,6,9,10,13,14,17,18,21,22), 0, 1)) %>%
      mutate(Y1A = na_if(Q1, 98), # history
             Y1B = na_if(Q4, 98), # biology
             Y1C = na_if(Q5, 98), # physics
             Y1D = na_if(Q6, 98), # English
             Y1E = na_if(Q7, 98),
             Y1F = na_if(Q8, 98),
             Y1G = na_if(Q2, 98),
             Y1H = na_if(Q3, 98)) %>%
      mutate(Y1 = (Y1A + Y1B + Y1C + Y1D + Y1E + Y1F + Y1G + Y1H)/8)
d$PartyID7 <- d$PARTYID7
d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))


#### DATA STANDARDIZATION #####

Tess_044_clean <-
      d %>%
      mutate(StudyId = 044,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_044_clean %>%
      write_csv("../analysis/cleaned/TESS_044_COHEN_CLEAN.csv")


#### STUDY 45 ####

## IMPORTING AND TIDYING
d <- import("TESS_0045_BLAIR_RECR_VARS_STATA.dta")

d$Q3 <- na_if(d$Q3, 98) 

d <- d %>% 
      mutate(not_engage = case_when(
            !BLAIR %in% c(5, 11, 2, 8, 6, 12, 3, 9) ~ NA_real_, 
            BLAIR %in% c(5, 11, 2, 8) ~ 1,
            TRUE ~ 0),
            FM = case_when(
                  !BLAIR %in% c(5, 11, 2, 8, 6, 12, 3, 9) ~ NA_real_,
                  BLAIR %in% c(5, 6, 11, 12) ~ 1, 
                  TRUE ~ 0),
            Y1 = Q3, 
            D1 = FM,
            D2 = not_engage)
d <- d %>% 
      mutate(Y1 = Y1 / sd(Y1[D1 == 0 & D2 == 0], na.rm = T))

d$PartyID7 <- d$PARTYID7

##### DATA STANDARDIZATION #####

Tess_045_clean <-
      d %>%
      mutate(StudyId = 045,
             FORMULA = "Y1 ~ D1 * D2") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, D2, TNRFU, FORMULA)

Tess_045_clean %>%
      write_csv("../analysis/cleaned/TESS_045_BLAIR_CLEAN.csv")

#### STUDY 46 ####

## IMPORTING AND TIDYING
d <- import("TESS_0046_MARGOLIS_RECR_VARS_STATA.dta")

d <- d %>% 
      mutate(D1 = case_when(
            !is.na(Q1) ~ 0,
            !is.na(Q3) ~ 1,
            TRUE ~ NA_real_),
            Y1A = na_if(Q1, 98),
            Y1B = na_if(Q3, 98),
            Y1 = case_when(
                  Y1A == 2 ~ 0,
                  Y1A == 1 ~ 1,
                  Y1B == 2 ~ 0,
                  Y1B == 1 ~ 1),
            TNRFU = na_if(TNRFU, -1),
            PartyID7 = P_PARTYID,
            ATTEND = P_ATTEND)

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

#### DATA STANDARDIZATION ####


TESS_046_MARGOLIS_CLEANED <-
      d %>%
      mutate(StudyId = 046,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

TESS_046_MARGOLIS_CLEANED %>%
      write_csv("../analysis/cleaned/TESS_046_MARGOLIS_CLEANED.csv")  


#### STUDY 47 ####

## IMPORTING AND TIDYING
d <- import("TESS_0047_JAKUBIAK_RECR_VARS_STATA.dta")

d <- d %>% 
      mutate(Y1A = na_if(D2A, 98),
             Y1B = na_if(D2B, 98),
             Y1C = na_if(D2D, 98),
             Y1D = na_if(D2F, 98),
             Y1E = na_if(D2G, 98),
             Y1 = (Y1A + Y1B + Y1C + Y1D + Y1E)/5, 
             D1 = case_when(
                   COND == 2 ~ 0,
                   COND == 1 ~ 1))

d$PartyID7 <- d$PARTYID7

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_047_clean <-
      d %>%
      mutate(StudyId = 047,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_047_clean %>%
      write_csv("../analysis/cleaned/TESS_047_JAKUBIAK_CLEAN.csv")

#### STUDY 48 ####

## IMPORTING AND TIDYING
d <- import("TESS_0048_GRACE_RECR_VARS_STATA.dta")


d <- d %>% mutate(D1 = ifelse(P_EXP %in% c(1, 2, 3, 4, 9, 10, 11, 12), 0, 1))
d$refusetx <- d$Q1
d$required <- d$RND_01

d <- d %>% mutate(Y1 = case_when(
      refusetx == 1 & required == 0 ~ 1,
      refusetx == 2 & required == 0 ~ 2,
      refusetx == 3 & required == 0 ~ 3,
      refusetx == 4 & required == 0 ~ 4,
      
      refusetx == 4 & required == 0 ~ 1,
      refusetx == 3 & required == 0 ~ 2,
      refusetx == 2 & required == 0 ~ 3,
      refusetx == 1 & required == 0 ~ 4))

d$PartyID7 <- d$P_PARTYID7
d$EMPLOY <- d$EMPLOY1
d$ATTEND <- d$P_ATTEND
d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

#### DATA STANDARDIZATION #####

Tess_048_clean <-
      d %>%
      mutate(StudyId = 048,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, D1, Y1, TNRFU, FORMULA)

Tess_048_clean %>%
      write_csv("../analysis/cleaned/TESS_048_GRACE_CLEAN.csv")


#### STUDY 50 ####

## IMPORTING AND TIDYING
d <- import("TESS_0050_HEADLEY_RECR_VARS_STATA.dta")

d <- d %>%
      mutate(Y1A = na_if(Q1A, 98),
             Y1B = na_if(Q1B, 98),
             Y1C = na_if(Q1C, 98),
             Y1D = na_if(Q1D, 98),
             Y1E = na_if(Q1E, 98),
             Y1F = na_if(Q1F, 98),
             Y1G = na_if(Q1G, 98),
             Y1H = na_if(Q1H, 98),
             Y1I = na_if(Q1I, 98),
             Y3A = na_if(Q2A, 98),
             Y3B = na_if(Q2B, 98),
             Y3C = na_if(Q3, 98)) %>% 
      mutate(Y1 = (Y1A + Y1B + Y1D + Y1F + Y1H + Y1I)/6, 
             Y2 = (Y1C + Y1E + Y1G)/3, 
             Y3 = (Y3A + Y3B + Y3C)/ 3) %>% 
      mutate(D1 = ifelse(P_HEADLEY < 4, 0, 1))
d$PartyID7 <- d$P_PARTYID7
d$ATTEND <- d$P_ATTEND

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_050_clean <-
      d %>%
      mutate(StudyId = 050,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, Y2, Y3, D1, TNRFU, FORMULA)

Tess_050_clean %>%
      write_csv("../analysis/cleaned/TESS_050_HEADLEY_CLEAN.csv")


#### STUDY 51 ####

## IMPORTING AND TIDYING
d <- import("TESS_0051_ZHU_RECR_VARS_STATA.dta")

d <- d %>%
      mutate(Y1A = case_when(
            !is.na(Q4A) ~ Q4A,
            !is.na(Q10A) ~ Q10A),
            Y1B = case_when(
                  !is.na(Q4B) ~ Q4B,
                  !is.na(Q10B) ~ Q10B),
            Y1C = case_when(
                  !is.na(Q4C) ~ Q4C,
                  !is.na(Q10C) ~ Q10C)) %>%
      mutate(Y1A = na_if(Y1A, 98), # coding missing as NA for scale construction
             Y1B = na_if(Y1B, 98),
             Y1C = na_if(Y1C, 98)) %>% 
      mutate(Y1 = (Y1A + Y1B + Y1C)/3) %>%
      mutate(D1 = case_when(
            RND_00 == 1 ~ 1,
            RND_00 == 2 ~ 0,
            RND_00 == 3 ~ NA_real_))
d$PartyID7 <- d$PARTYID

psych::alpha(dplyr::select(d, Y1A, Y1B, Y1C)) # tests scale reliability (.80)

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_051_clean <-
      d %>%
      mutate(StudyId = 051,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_051_clean %>%
      write_csv("../analysis/cleaned/TESS_051_ZHU_CLEAN.csv")


#### STUDY 52 ####

## IMPORTING AND TIDYING
d <- import("TESS_0052_HOLLIN_RECR_VARS_STATA.dta")

d$Q4 <- na_if(d$Q4, 98)
d <- d %>% 
      mutate(Y1 = Q4) %>%
      mutate(D1 = case_when(
            P_TRANSP == 1 ~ 0,
            P_TRANSP == 2 ~ 1,
            TRUE ~ NA_real_
      ))
d$PartyID7 <- d$P_PARTYID
d$ATTEND <- d$P_ATTEND

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

#### DATA STANDARDIZATION #####

Tess_052_clean <-
      d %>%
      mutate(StudyId = 052,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_052_clean %>%
      write_csv("../analysis/cleaned/TESS_052_HOLLIN_CLEAN.csv")


#### STUDY 53 ####

## IMPORTING AND TIDYING
d <- import("TESS_0053_STOKER_RECR_VARS_STATA.dta")

d <- d %>% mutate(D1 = ifelse(DOV_COND %in% c(1,3,5,7,9,11,13,15), 1, 0)) %>% # loss-framed = 1
      mutate(ANGER = na_if(ANGER, 98)) %>%
      mutate(Y1 = ANGER)
d$PartyID7 <- d$PARTYID

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_053_clean <-
      d %>%
      mutate(StudyId = 053,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_053_clean %>%
      write_csv("../analysis/cleaned/TESS_053_STOKER_CLEAN.csv")

#### STUDY 54 ####

## IMPORTING AND TIDYING
d <- import("TESS_0054_WEISSHAAR_RECR_VARS_STATA.dta")

d$CAP <- na_if(d$CAP, 98)
d <- d %>%
      mutate(D1A = factor(P_NAME_TREAT, levels = c(3, 1, 2, 4), 
                          labels = c("WM", "BM", "BF", "WF")), #WM = white male, etc.
             D1 = case_when(
                   D1A == "WM" ~ 0,
                   D1A == "BM" ~ 1,
                   TRUE ~ NA_real_
             ),
             D2 = case_when(
                   D1 == "WM" ~ 0,
                   D1 == "WF" ~ 1,
                   TRUE ~ NA_real_
             ),
             Y1 = CAP)
d$PartyID7 <- d$P_PARTYID
d$ATTEND <- d$P_ATTEND

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

##### DATA STANDARDIZATION #####

Tess_054_clean <-
      d %>%
      mutate(StudyId = 054,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, D2, TNRFU, FORMULA)

Tess_054_clean %>%
      write_csv("../analysis/cleaned/TESS_054_WEISSHAAR_CLEAN.csv")


#### STUDY 56 ####

## IMPORTING AND TIDYING
d <- import("TESS_0056_BAI_RECR_VARS_STATA.dta")

d <- d %>%
      mutate(D1 = ifelse(P_CON == 2, 1, 0),
             Y1 = na_if(Q4, 98),
             Y2 = na_if(Q3, 98),
             Y3 = na_if(Q2, 98) - 1)
d$PartyID7 <- d$P_PARTYID7
d$ATTEND <- d$P_ATTEND

d <- d %>% mutate(Y1 = Y1 / sd(Y1[D1 == 0], na.rm = T))

#### DATA STANDARDIZATION #####

Tess_056_clean <-
      d %>%
      mutate(StudyId = 056,
             FORMULA = "Y1 ~ D1") %>%
      select(StudyId, WEIGHT, CaseId, AGE, GENDER, RACETHNICITY,EDUC, MARITAL,
             EMPLOY, INCOME, STATE, METRO, INTERNET, PartyID7, ATTEND,
             HOUSING, HOME_TYPE, HOME_TYPE, PHONESERVICE, HHSIZE, 
             HH01, HH25, HH612, HH1317, HH18OV, Y1, D1, TNRFU, FORMULA)

Tess_056_clean %>%
      write_csv("../analysis/cleaned/TESS_056_BAI_CLEAN.csv")

